import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  {
    name: "article",
    path: "/article/:id",
    component: () =>
      import(/* webpackChunkName: "article" */ "@/views/Article.vue"),
    props: (route) => ({
      article: route.params.article,
      idNum: route.params.id,
    }),
  },
  {
    name: "stock",
    path: "/stock",
    component: () =>
      import(/* webpackChunkName: "stock" */ "@/views/Stock.vue"),
  },
  {
    name: "fund",
    path: "/fund",
    component: () => import(/* webpackChunkName: "fund" */ "@/views/Fund.vue"),
  },
  {
    name: "me",
    path: "/me",
    component: () => import(/* webpackChunkName: "me" */ "@/views/Me.vue"),
  },
  {
    name: "login",
    path: "/login",
    component: () =>
      import(/* webpackChunkName: "login" */ "@/views/Login.vue"),
  },
];

const router = new VueRouter({
  routes,
  scrollBehavior(to, from, savePosition) {
    if (to.path === "/article/" + to.params.id) {
      return { x: 0, y: 0 };
    }
    if (to.path === "/") {
      return savePosition;
    }
    // 会记录滚动条的坐标, 是在点击前进和后退的时候记录
    // console.log(savePosition);
  },
});

export default router;
